package com.eascs.web.p2p.action;



import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.common.entity.LoaneeEntity;
import com.eascs.p2p.common.service.IInvestObjectService;
import com.eascs.p2p.common.service.ILoaneeService;
import com.eascs.p2p.common.vo.InvestObjectVO;
import com.eascs.p2p.common.vo.LoaneeVO;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;

@RestController
@RequestMapping("/loanerRest")
public class LoanerRestController extends BaseRestController {
	
	@Reference
	ILoaneeService loaneeService;

	@Reference
	IInvestObjectService investObjectService;
	@RequestMapping(value="/editLoaner")
	public BaseRestResult<String>  editLoaner(LoaneeEntity vo) {
		BaseRestResult<String> result = null;
		LoaneeVO loaneeVO=new LoaneeVO();
		loaneeVO.setId(vo.getId());
		loaneeVO.setMobile(vo.getMobile());
		if(vo.getId() != null){
			loaneeVO.setOperType(2);
		}
		ModelResult<Integer> mobile_mode=loaneeService.getLoaneeEntity(loaneeVO);
		if(mobile_mode.isSuccess()){
			if(mobile_mode.getModel()>0){
				
				return result = BaseRestResult.error("手机号不能重复");
			}
			
		}
		
		loaneeVO=new LoaneeVO();
		loaneeVO.setId(vo.getId());
		loaneeVO.setIdCardNo(vo.getIdCardNo());
		if(vo.getId() != null){
			loaneeVO.setOperType(2);
		}
		ModelResult<Integer> idCardNo_mobile=loaneeService.getLoaneeEntity(loaneeVO);
		if(idCardNo_mobile.isSuccess()){
			if(idCardNo_mobile.getModel()>0){
				
			return	result = BaseRestResult.error("身份证号码不能重复");
			}
			
		}
		if (vo.getId() == null) { //新增借款人
			ModelResult<String> model = loaneeService.addLoaneeEntity(vo);
			if (model.isSuccess()) {
				if (StringUtil.isNotBlank(model.getModel())) {
					result = BaseRestResult.warn("添加成功").andRedirect("/loaner/index");
					
				}else{
					result = BaseRestResult.error("添加失败");
				}

			}else{
				
				result = BaseRestResult.error("添加失败,数据库发生异常");
			}
			
		}else {
			ModelResult<Boolean> model = loaneeService.updateLoaneeEntity(vo);
			if (model.isSuccess()) {
				if (model.getModel()) {
					result = BaseRestResult.warn("修改成功").andRedirect("/loaner/index");
					
				}else{
					result = BaseRestResult.error("修改失败");
				}

			}else{
				
				result = BaseRestResult.error("修改失败,数据库发生异常");
			}
			
		}
		
		
		return result;
	}
	
	
	@RequestMapping(value = "/loaner_detail")
	public HtmlRestResult detail(String id) {
		InvestObjectVO vo=new InvestObjectVO();
		vo.setType(2);
		vo.setPageSize(10);
		vo.setLoaneeId(id);
		ModelResult<PageResult<Object[]>> model = investObjectService.findPageInvestObjectEntity(vo);
		PageResult<Object[]> pageResult=new PageResult<Object[]>();
		if(model.isSuccess()){
			 pageResult = model.getModel();
		} 
			
		

		String[] titles = {"项目编号" ,"项目名称","项目类型", "借款人姓名","借款金额", "年化率", "借款期限", "失效时间" ,"融资进度" ,"状态"};
		
		String pageUrl = "/loanerRest/loaner_detail";

		
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl + "&showPage=");
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		//paginationInfo.setMoreButtons(buttons);
		ViewContext.put("paginationInfo", paginationInfo);
		
		
	
		
		ViewContext.put("title", "借款详情");
		return HtmlRestResult.popHtmlFragment("/loaner/loaner_detail");
		
	}

}
